home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / Programming / QuakeTools / src / util / ppmdiff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-05-27  |  2.1 KB  |  93 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main(int argc, char **argv) {
  5.   FILE *inFile1 = 0;
  6.   FILE *inFile2 = 0;
  7.   FILE *outFile = 0;
  8.   char *outName = 0;
  9.   char ppmString[256];
  10.   int t1, t2, x1, x2, y1, y2, i, max1, max2;
  11.   long long int mse = 0;
  12.   double psnr = 0;
  13.   
  14.   if((argc > 5) || (argc == 4)) {
  15.     printf("too much arguments\n");
  16.     return 5;
  17.   }
  18.   
  19.   for (i = 1; i < argc; i++) {
  20.     if (!strcmp(argv[i], "-o")) {
  21.       outName = argv[++i];
  22.       if(!(outFile = fopen(outName, "w"))) {
  23.         printf("failed to open outputfile %s\n", outName);
  24.         return 10;
  25.       }
  26.     }
  27.     else if(!inFile1) {
  28.       if(!(inFile1 = fopen(argv[i], "r"))) {
  29.         printf("failed to open first inputfile %s\n", argv[i]);
  30.         return 10;
  31.       }
  32.     }
  33.     else if(!inFile2) {
  34.       if(!(inFile2 = fopen(argv[i], "r"))) {
  35.         printf("failed to open second inputfile %s\n", argv[i]);
  36.         return 10;
  37.       }
  38.     }
  39.   }
  40.   
  41.   if(!inFile1 || !inFile2) {
  42.     printf("you must specify two sources\n");
  43.     return 5;
  44.   }
  45.   if(!outFile)
  46.     outFile = stdout;
  47.   
  48.   fscanf(inFile1, "P%1d\n%d %d\n%d\n", &t1, &x1, &y1, &max1);
  49.   fscanf(inFile2, "P%1d\n%d %d\n%d\n", &t2, &x2, &y2, &max2);
  50.   
  51.   if((t1 != t2) || (x1 != x2) || (y1 != y2) || (max1 != max2)) {
  52.     printf("cannot compare pictures with different sizes or formats\n");
  53.     return 5;
  54.   }
  55.   if(!((t1 == 5) || (t1 == 6))) {
  56.     printf("cannot handle this format P%1d\n", t1);
  57.     return 5;
  58.   }
  59.   
  60.   fprintf(outFile, "P5\n%d %d\n255\n", x1, y1);
  61.   
  62.   for(i = 0; i < (x1 * y1); i++) {
  63.     int r, g, b, p;
  64.     
  65.     r = fgetc(inFile2) - fgetc(inFile1);
  66.     mse += (long long int)(r*r);
  67.     r >>= 1;
  68.     r += 127;
  69.     if(t1 == 6) {
  70.       g  = fgetc(inFile2) - fgetc(inFile1);
  71.       mse += (long long int)(g*g);
  72.       g >>= 1;
  73.       g += 127;
  74.       b  = fgetc(inFile2) - fgetc(inFile1);
  75.       mse += (long long int)(b*b);
  76.       b >>= 1;
  77.       b += 127;
  78.       
  79.       p = (r + g + b) / 3;
  80.       fputc(p, outFile);
  81.     }
  82.     else
  83.       fputc(r, outFile);
  84.   }
  85.   
  86.   psnr = 10 * log10(65536.0 / ((double)mse / (x1 * y1 * (t1 == 6 ? 3 : 1))));
  87.   printf("psnr: %g\n", psnr);
  88.   
  89.   fclose(inFile1);
  90.   fclose(inFile2);
  91.   fclose(outFile);
  92. }
  93.